from django.db import models


# Create your models here.

class Admins(models.Model):
    name = models.CharField(max_length=50, blank=True, null=True)
    email = models.CharField(unique=True, max_length=255)
    encrypted_password = models.CharField(max_length=255)
    sign_in_count = models.IntegerField()
    current_sign_in_at = models.DateTimeField(blank=True, null=True)
    last_sign_in_at = models.DateTimeField(blank=True, null=True)
    current_sign_in_ip = models.CharField(max_length=255, blank=True, null=True)
    last_sign_in_ip = models.CharField(max_length=255, blank=True, null=True)
    failed_attempts = models.IntegerField()
    unlock_token = models.CharField(max_length=255, blank=True, null=True)
    locked_at = models.DateTimeField(blank=True, null=True)
    created_at = models.DateTimeField()
    updated_at = models.DateTimeField()
    can_delete_babies = models.IntegerField(blank=True, null=True)
    can_delete_parents = models.IntegerField(blank=True, null=True)
    can_delete_content = models.IntegerField(blank=True, null=True)
    can_view_analytics = models.IntegerField(blank=True, null=True)
    can_view_babies = models.IntegerField(blank=True, null=True)
    can_view_parents = models.IntegerField(blank=True, null=True)
    can_view_users = models.IntegerField(blank=True, null=True)
    can_view_content = models.IntegerField(blank=True, null=True)
    can_view_category = models.IntegerField(blank=True, null=True)
    can_view_activities = models.IntegerField(blank=True, null=True)
    can_view_families = models.IntegerField(blank=True, null=True)
    can_add_category = models.IntegerField(blank=True, null=True)
    can_add_babies = models.IntegerField(blank=True, null=True)
    can_add_content = models.IntegerField(blank=True, null=True)
    can_overwrite_discharge = models.IntegerField(blank=True, null=True)
    role = models.CharField(max_length=50, blank=True, null=True)
    hospital_name = models.CharField(max_length=50, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'admins'

    def admins_to_json(self):
        result = {}
        result["email"] = self.email if self.id else ""
        # result["encrypted_password"] = self.encrypted_password if self.encrypted_password else ""
        # result["sign_in_count"] = self.sign_in_count if self.sign_in_count else ""
        # result["current_sign_in_at"] = self.current_sign_in_at.strftime(
        #     '%Y-%m-%d %H:%M:%S') if self.current_sign_in_at else ""
        # result["last_sign_in_at"] = self.last_sign_in_at.strftime('%Y-%m-%d %H:%M:%S') if self.last_sign_in_at else ""
        result["current_sign_in_ip"] = self.current_sign_in_ip if self.current_sign_in_ip else ""
        result["last_sign_in_ip"] = self.last_sign_in_ip if self.last_sign_in_ip else ""
        # result["failed_attempts"] = self.failed_attempts if self.failed_attempts else ""
        # result["unlock_token"] = self.unlock_token if self.unlock_token else ""
        # result["locked_at"] = self.locked_at.strftime('%Y-%m-%d %H:%M:%S') if self.locked_at else ""
        return result


class Analytics(models.Model):
    user_id = models.IntegerField(blank=True, null=True)
    screen_name = models.CharField(max_length=255, blank=True, null=True)
    screen_action = models.CharField(max_length=255, blank=True, null=True)
    client = models.CharField(max_length=255, blank=True, null=True)
    event_details = models.TextField(blank=True, null=True)
    application_version = models.CharField(max_length=255, blank=True, null=True)
    clientos = models.CharField(db_column='clientOS', max_length=255, blank=True,
                                null=True)  # Field name made lowercase.
    clientmodel = models.CharField(db_column='clientModel', max_length=255, blank=True,
                                   null=True)  # Field name made lowercase.
    ip_address = models.CharField(max_length=255, blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    updated_at = models.DateTimeField(auto_now=True, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'analytics'


class AdminUser(models.Model):
    name = models.CharField(max_length=50, blank=True, null=True)
    email = models.CharField(unique=True, max_length=255)
    encrypted_password = models.CharField(max_length=255, blank=True, null=True)
    hospital = models.CharField(max_length=60, blank=True, null=True)
    role = models.ForeignKey(
        to='Role',
        db_constraint=False,
        on_delete=models.CASCADE
    )

    class Meta:
        db_table = 'adminuser'
        verbose_name = "adminuser information"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

    @property
    def role_name(self):
        return self.role.title


class Role(models.Model):
    title = models.CharField(max_length=32, verbose_name="角色名称")

    class Meta:
        managed = False
        db_table = 'role'
        verbose_name = "role information"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


class Permission(models.Model):
    title = models.CharField(max_length=32, verbose_name="权限描述")
    url = models.CharField(max_length=128, verbose_name="URL")
    role = models.ForeignKey(
        to='Role',
        db_constraint=False,
        on_delete=models.CASCADE
    )

    class Meta:
        db_table = 'permission'
        verbose_name = "permission information"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


class Tag(models.Model):
    tag_title = models.CharField(max_length=100, verbose_name="文章标签")

    class Meta:
        db_table = 'tag'
        verbose_name = "tag information"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.tag_title

    def tag_tojson(self):
        ret = {
            "id": self.id,
            "tag_title": self.tag_title
        }
        return ret


class DBDumpRecord(models.Model):
    file_path = models.CharField(max_length=255, verbose_name="文件路径")
    file_url = models.CharField(max_length=255, verbose_name="文件URL")
    cost_time = models.IntegerField(verbose_name="消耗时间（sec）")
    created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    updated_at = models.DateTimeField(auto_now=True, blank=True, null=True)

    class Meta:
        db_table = 'db_dump_record'
        managed = False
